<?php

defined( 'IN_CLASS' ) or die( 'Access Error!' );

class ModelOnline extends base {
	
	public function getall() {
		$sql = "select t2.id, t2.name from $this->tonline as t1, $this->tmember as t2 where t1.member=t2.id";
		$aOnline = model::dbuser()->get_all( $sql );
		return $aOnline;
	}
	
	/**
	 * 删除已经不在线的用户
	 * @return int
	 */
	public function deloffline() {
		$now = time();          //当前时间
		$timeout = 300;			//单位：秒,上次刷新后$timeout时间内算该成员在线上
		
		$sql = "delete from $this->tonline where time < ". ( $now - $timeout );
		model::dbuser()->exe( $sql );
		$flag = model::dbuser()->update_rows();
		
		return $flag;
	}
	
	/**
	 * 刷新个人的在线资料
	 * @param string $uid
	 * @return int
	 */
	public function fresh( $uid ) {
		if( !strlen( $uid ) ) {
			return 0;
		}
		$now = time();
		$sql = "INSERT INTO $this->tonline SET member='{$uid}', time={$now} ON DUPLICATE KEY UPDATE time='{$now}'";
		model::dbuser()->exe( $sql );
		$flag = model::dbuser()->update_rows();
		
		return $flag;
	}
}